//
//  RegisterViewController.m
//  baokaodaxue
//
//  Created by dangyangyang on 15/5/25.
//  Copyright (c) 2015年 xxx. All rights reserved.
//

#import "RegisterViewController.h"
#import "RegisterViewModel.h"
@interface RegisterViewController ()
@property (weak, nonatomic) IBOutlet UITextField *phoneTextField;
@property (weak, nonatomic) IBOutlet UITextField *passwordTextField;
@property (weak, nonatomic) IBOutlet UITextField *verityCodeTextField;

@end

@implementation RegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    [_phoneTextField becomeFirstResponder];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
- (IBAction)verityCode:(id)sender {
    if ([_phoneTextField.text length]!=11) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请输入正确的手机号码" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
        [alertView show];
        
        return;
    }
    
    [RegisterViewModel verifyCodeByPhone:_phoneTextField.text
                                 success:^{
                                     UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示"
                                                                                         message:@"验证码已发送，注意查收"
                                                                                        delegate:nil
                                                                               cancelButtonTitle:@"好的"
                                                                               otherButtonTitles:nil, nil];
                                     [alertView show];

    }
                                 failure:^(NSString *message) {
                                     UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示"
                                                                                         message:message
                                                                                        delegate:nil
                                                                               cancelButtonTitle:@"好的"
                                                                               otherButtonTitles:nil, nil];
                                     [alertView show];

    }];
}
- (IBAction)p_login:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)p_back:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)p_register:(id)sender {
    if ([_phoneTextField.text length]!=11) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请输入正确的手机号码" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
        [alertView show];
        
        return;
    }
    if ([_passwordTextField.text length]==0) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请输入密码" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
        [alertView show];
        
        return;
    }
    
    if ([_verityCodeTextField.text length]==0) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示"
                                                            message:@"请输入验证码"
                                                           delegate:nil
                                                  cancelButtonTitle:@"好的"
                                                  otherButtonTitles:nil, nil];
        [alertView show];
        
        return;
    }
    
    __weak RegisterViewController *weakSelf = self;
   [RegisterViewModel registerByPhone:_phoneTextField.text
                             password:_passwordTextField.text
                           verifycode:_verityCodeTextField.text success:^{
        
                               dispatch_queue_t mainQueue = dispatch_get_main_queue();
                               dispatch_async(mainQueue, ^(void) {
                                   [weakSelf dismissViewControllerAnimated:YES completion:nil];

                               });
             
       
   } failure:^(NSString *message) {
       UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示"
                                                           message:message
                                                          delegate:nil
                                                 cancelButtonTitle:@"好的"
                                                 otherButtonTitles:nil, nil];
       [alertView show];
       

   }];


}

@end
